package BlockingQueueDemo;

import lombok.SneakyThrows;

import java.util.concurrent.BlockingQueue;

/**
 * @author caihe
 * @date 2021/6/30 9:33
 */
public class MyConsumer implements Runnable{

    private BlockingQueue<String> blockingQueue;

    public MyConsumer(BlockingQueue<String> blockingQueue) {
        this.blockingQueue = blockingQueue;
    }
    
    @SneakyThrows
    @Override
    public void run() {
        while(true) {
            // 消费耗时
            System.out.println("A准备消费，库存为：" + blockingQueue.size());
            blockingQueue.take();
            System.out.println("A消费后，库存为：" + blockingQueue.size());
            Thread.sleep(400);

            System.out.println("B准备消费，库存为：" + blockingQueue.size());
            blockingQueue.take();
            System.out.println("B消费后，库存为：" + blockingQueue.size());
            Thread.sleep(400);
            
        }
    }
}
